package com.jiot.plugin.qiniu;

import java.util.Map;
import com.google.gson.Gson;
import com.hazelcast.util.MapUtil;
import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.Region;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;
import com.qiniu.util.StringMap;

public class QiniuKit {
	
	private static Auth auth;
	private static long expireSeconds;
	private static String syhost;
	private static String gghost;
	
	static void init(Auth _auth, long _expireSeconds, String _syhost, String _gghost){
		auth = _auth;
		expireSeconds = _expireSeconds;
		syhost = _syhost;
		gghost = _gghost;
	}
	
	public static DefaultPutRet up(String bucket, String key, String localFilePath, StringMap putPolicy) throws QiniuException {
		Configuration cfg = new Configuration(Region.region0());
		UploadManager uploadManager = new UploadManager(cfg);
		String upToken = auth.uploadToken(bucket, key, expireSeconds, putPolicy);
	    Response response = uploadManager.put(localFilePath, key, upToken);
	    return new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
	}
	
	public static String getToken(String bucket, String key, StringMap putPolicy) {
		String upToken = auth.uploadToken(bucket, key, expireSeconds, putPolicy);
		return upToken;
	}
	
	public static String getToken(String bucket) {
		String upToken = auth.uploadToken(bucket);
		return upToken;
	}
	
	public static Map<Integer, String> getTokens(String bucket, String[] keys, StringMap putPolicy) {
		Map<Integer, String> images = MapUtil.createHashMap(keys.length);
		for (int j = 0; j < keys.length; j++){
			String upToken = auth.uploadToken(bucket, keys[j], expireSeconds, putPolicy);//
			images.put(j, upToken);
		}
		return images;
	}
	
	//删除文件
    public static Response deleteFileFromQiniu(String[] keyList, String bucketname) throws QiniuException{
    	Configuration cfg = new Configuration(Region.region0());
        BucketManager bucketManager = new BucketManager(auth, cfg);
        BucketManager.BatchOperations batchOperations = new BucketManager.BatchOperations();
        batchOperations.addDeleteOp(bucketname, keyList);
        return bucketManager.batch(batchOperations);
    }
    
    //下载
    public static String getDownloadUrl(String fileUrl) {
        String finalUrl = auth.privateDownloadUrl(fileUrl, expireSeconds);
		return finalUrl;
    }
    
    //移动
    public static Response move(String fromBucket, String[] fromKeys, String toBucket, String[] toKeys, Boolean force) throws QiniuException {
    	Configuration cfg = new Configuration(Region.region0());
    	BucketManager bucketManager = new BucketManager(auth, cfg);
    	BucketManager.BatchOperations batchOperations = new BucketManager.BatchOperations();
    	for (int i=0; i< fromKeys.length; i++) {
            batchOperations.addMoveOp(fromBucket, fromKeys[i], toBucket, toKeys[i]);
        }
		return bucketManager.batch(batchOperations);
    }
  
    //复制
    public static Response copy(String fromBucket, String[] fromKeys, String toBucket, String[] toKeys, Boolean force) throws QiniuException {
		Configuration cfg = new Configuration(Region.region0());
    	BucketManager bucketManager = new BucketManager(auth, cfg);
    	BucketManager.BatchOperations batchOperations = new BucketManager.BatchOperations();
    	for (int i=0; i< fromKeys.length; i++) {
            batchOperations.addCopyOp(fromBucket, fromKeys[i], toBucket, toKeys[i]);
        }
		return bucketManager.batch(batchOperations);
    }
    
    
    
    
	
	
	public static Auth getAuth() {
		return auth;
	}

	public static void setAuth(Auth auth) {
		QiniuKit.auth = auth;
	}

	public static long getExpireSeconds() {
		return expireSeconds;
	}

	public static void setExpireSeconds(long expireSeconds) {
		QiniuKit.expireSeconds = expireSeconds;
	}

	public static String getSyhost() {
		return syhost;
	}

	public static void setSyhost(String syhost) {
		QiniuKit.syhost = syhost;
	}

	public static String getGghost() {
		return gghost;
	}

	public static void setGghost(String gghost) {
		QiniuKit.gghost = gghost;
	}


	
	
	
	
	
	
}
